//游戏资源加载器
module resLoading {
    let loadingView: gameLoadingUI;
    let time = null;
    let name = "";
    export function loadResGroup(groupName: string) {
        name = "";
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.DDZ) {
            name = "ddz";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
            name = "brnn";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.QZNN) {
            name = "qznn";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.HLSZ) {
            name = "hlsz";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.WJL) {
            name = "wjl";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.LHD) {
            name = "lhd";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
            name = "lhd";
        }
        if (groupName == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
            name = "brebg";
        }
        if (name == "") {
            return;
        }
        if (RES.isGroupLoaded(name)) {
            //发送登陆消息
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.GAME_RES_LOAD);
            return;
        }
        headLoadUi();
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, onResourceLoadErr, this);
        onResourceProgress();
        loadingView = new gameLoadingUI();
        GameLayerManager.gameLayer().loadLayer.addChild(loadingView);
        RES.setMaxLoadingThread(4)
        RES.loadGroup(name, 0, loadingView);
    }
    //资源组加载完成
    export function onResourceLoadComplete() {
        //发送登陆消息
        egret.clearTimeout(time)
        headLoadUi();
        game.AppFacade.instance.sendNotification(PanelNotify.HALL.GAME_RES_LOAD);
    }
    //资源组加载进度发生变化
    export function onResourceProgress() {
        egret.clearTimeout(time)
        time = egret.setTimeout(() => {
            onResourceLoadErr()
        }, this, 5000)
    }
    //资源组加载失败
    export function onResourceLoadErr() {
        RES.destroyRes(name);
        let t = egret.setInterval(() => {
            if (!RES.isGroupLoaded(name)) {
                egret.clearInterval(t)
                RES.loadGroup(name, 0, loadingView);
                onResourceProgress();
            }
        }, this, 50)
    }

    export function headLoadUi() {
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, onResourceLoadComplete, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, onResourceProgress, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, onResourceLoadErr, this);
        if (GameLayerManager.gameLayer().loadLayer.contains(loadingView)) {
            GameLayerManager.gameLayer().loadLayer.removeChild(loadingView);
        }
    }
}